<template>
  <el-main>
    <h2>反馈回复</h2>
    <el-row v-for="{ message, userId } in chatList" style="margin-top: 10px;">
      <el-col>
        <el-card shadow="hover">
          <span style="font-size: large;color:cadetblue;">用户ID: {{ userId }}</span>
          <br>
          <span style="font-size: larger;">{{ message }}</span>
          <br />
          <el-button type="primary" @click="openDialog(userId, message)" style="float: right;">回复</el-button>
        </el-card>
      </el-col>
    </el-row>
    <div>

      <el-dialog title="回复消息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="消息内容">
            <span>
              {{ form.oldMessage }}
            </span>
            <el-input v-model="form.message" type="textarea" :autosize="{ minRows: 4, maxRows: 10 }" autocomplete="off">
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSubmit(form.userId)">发送</el-button>
        </div>
      </el-dialog>

    </div>


  </el-main>



</template>

<script>
import { getChatList, chatSend } from "@/api/chatroom/chat";
import { get } from "sortablejs";
export default {
  data() {
    return {
      dialogFormVisible: false,
      form: {
        oldMessage: "",
        message: "",
        userId: ""
      },
      chatList: []
    }
  },
  methods: {
    openDialog(userId, oldMessage) {
      this.dialogFormVisible = true;
      this.form.userId = userId;
      this.form.message = "";
      this.form.oldMessage = oldMessage;
    },
    handleSubmit(userId) {
      let message = this.form.message;
      let oldMessage = this.form.oldMessage;
      console.log(userId, message, oldMessage)
      chatSend(userId, message, oldMessage).then(response => {
        this.dialogFormVisible = false;
        this.form.message = "";
        // 刷新页面
        location.reload();
        console.log(response)
      })
    },
    getChats() {
      getChatList().then(response => {
        let list = JSON.parse(response.data)
        list.forEach(item => {
          let o = JSON.parse(item)
          console.log(o)
          this.chatList.push({ message: o.message, userId: o.userId })
        })

      })
    },
  },
  mounted() {
    this.getChats();
  },
}
</script>